﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System.IO;
using System.Configuration;

namespace EIA.Basic.DAL.IBatisMapping
{
    public class SqlMapperHelper
    {
        public const string CONFIG_KEY_SQLMAPCONFIG_LOCATION = "EIA.Basic.DAL.iBatisNet.SqlMapConfigLocation";
        public const string CONFIG_KEY_PROVIDER = "EIA.Basic.DAL.iBatisNet.Provider";
        public const string CONFIG_KEY_CONNECTIONGSTRING = "EIA.Basic.DAL.iBatisNet.ConnectionString";

        private static ISqlMapper _SqlMapper = null;
        private static Stream _SqlMapConfigStream = null;

        public static ISqlMapper GetSqlMapper()
        {
            try
            {
                if (_SqlMapper == null) NewSqlMapper();
                return _SqlMapper;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static ISqlMapper NewSqlMapper()
        {
            try
            {
                ISqlMapper r = null;
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                if (null == _SqlMapConfigStream)
                {
                    lock (IsLock)
                    {
                        if (null == _SqlMapConfigStream)
                        {
                            string sqlMapLocation = DotNETX.Utility.Config.AppConfigUtils.TryGetSetting<string>(System.Reflection.Assembly.GetExecutingAssembly(), CONFIG_KEY_SQLMAPCONFIG_LOCATION, "eiacommon_sqlmap.config");
                            using (Stream s = DotNETX.Utility.Resources.FileLoader.GetStream(sqlMapLocation))
                            {
                                using (StreamReader tr = new StreamReader(s))
                                {
                                    string configContent = tr.ReadToEnd();
                                    string assemblyDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                                    string provider = DotNETX.Utility.Config.AppConfigUtils.TryGetSetting<string>(System.Reflection.Assembly.GetExecutingAssembly(), CONFIG_KEY_PROVIDER);
                                    string connString = DotNETX.Utility.Config.AppConfigUtils.TryGetSetting<string>(System.Reflection.Assembly.GetExecutingAssembly(), CONFIG_KEY_CONNECTIONGSTRING);
                                    configContent = configContent.Replace("$${assemblyPath}", assemblyDirectory);
                                    configContent = configContent.Replace("$${provider}", provider);
                                    configContent = configContent.Replace("$${connectionString}", connString);
                                    _SqlMapConfigStream = new MemoryStream();
                                    StreamWriter sw = new StreamWriter(_SqlMapConfigStream);
                                    sw.Write(configContent);
                                    sw.Flush();
                                }
                            }
                        }
                    }
                }
                if (_SqlMapConfigStream.Position > 0) _SqlMapConfigStream.Seek(0, SeekOrigin.Begin);
                r = builder.Configure(_SqlMapConfigStream);
                lock (IsLock)
                {
                    _SqlMapper = r;
                }
                return r;
            }
            catch (Exception ex)
            {
                // todo: log
                return null;
            }
        }

        private static object IsLock = new object();
    }
}
